package logic

import (
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"context"
	"github.com/jinzhu/copier"

	"MyBlogv2/Servers/Article/article"
	"MyBlogv2/Servers/Article/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetTagListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetTagListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTagListLogic {
	return &GetTagListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetTagListLogic) GetTagList(in *Article.ArticleRequest) (*Article.GetTagListResponse, error) {
	var tagVoList []models.TagVo
	var tagList []models.Tag
	models.Db.Model(new(models.Tag)).Find(&tagList)
	copier.Copy(&tagVoList, &tagList)
	for i, tag := range tagList {
		var count int64
		models.Db.Model(new(models.ArticleTag)).Where("tag_id = ?", tag.Id).Count(&count)
		tagVoList[i].ArticleCount = int(count)
	}
	var tagVoListResponse []*Article.TagVo
	copier.Copy(&tagVoListResponse, &tagVoList)
	for i, v := range tagVoList {
		tagVoListResponse[i].CreateTime = helper.MyTimeToTimestamp(v.CreateTime)
		tagVoListResponse[i].UpdateTime = helper.MyTimeToTimestamp(v.UpdateTime)
	}
	return &Article.GetTagListResponse{
		Code:      int64(HttpCode.SUCCESS),
		TagVoList: tagVoListResponse,
	}, nil
}
